package com.example.kimasi.recite.manager;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.database.sqlite.SQLiteDatabase;

import com.example.kimasi.recite.R;
import com.example.kimasi.recite.aaatools.LogUtils;

import org.xmlpull.v1.XmlPullParserException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 一些本地资源的获取,xml解析
 * Created by kimasi on 2016/4/7.
 */
public class ResourcesAccess {

    private static ResourcesAccess resourcesAccess;

    private Context context;

    private ResourcesAccess(Context context) {
        this.context = context;

    }

    public static ResourcesAccess getInstance(Context context) {
        if (resourcesAccess == null) {
            resourcesAccess = new ResourcesAccess(context);
        }
        return resourcesAccess;
    }

    /**
     * 初始化数据到数据库
     *
     * @param db
     */
    public void initXmltoDatabase(SQLiteDatabase db) {
        String en = null, cn = null, proficiency = "0", plan = "0", index = "0";
        int j = 0, h = 0;
        if (context != null) { //初始化一些表数据
            try {
                XmlResourceParser xmlResourceParser = context.getResources().getXml(R.xml.youdao);
                while (xmlResourceParser.getEventType() != XmlResourceParser.END_DOCUMENT) {
                    if (xmlResourceParser.getEventType() == XmlResourceParser.START_TAG) {
                        String tagName = xmlResourceParser.getName();
                        if (tagName.equals("word")) {
                            en = xmlResourceParser.nextText();
                            j = 1;
                        } else if (tagName.equals("trans")) {
                            cn = xmlResourceParser.nextText();
                            h = 1;
                        }
                        if (j == 1 && h == 1) {
                            db.execSQL("insert into dict values(null , ? , ?,?,?,?)" //每个项都要有初始化
                                    , new String[]{en, cn, proficiency, plan, index});
                            j = 0;
                            h = 0;
                        }
                    }
                    xmlResourceParser.next();
                }
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    String EMPTY="";
    String string = "" +
            "";

    public void initTxtToDatabase(SQLiteDatabase db) {
        InputStream is = context.getResources().openRawResource(R.raw.chuzhong);
        InputStreamReader inputStreamReader = new InputStreamReader(is);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        try {
            int i=0;
            Boolean countEn = true, countCn = false;
            String string1, en = "", cn = "", proficiency = "0", plan = "0", index = "0"; //inde单词的分类,0是计算机,1是初中单词
            while ((string1 = bufferedReader.readLine()) != null) { // 测试成功,读取一行数据
                if (!string1.equals(string)) {
                    if (countCn) {
                        cn += string1 + "\n";
                    }
                    if (countEn) {
                        en = string1;
                        cn="";
                        countEn = false;
                        countCn = true;
                    }
                } else {
                    countEn = true;
                    countCn = false;
                    LogUtils.v2((++i)+ en + "" + cn); // 可以两个一组存入数据库
                    if (!cn.isEmpty()){ //不是空的,可以存入数据库
                        db.execSQL("insert into dict values(null , ? , ?,?,?,?)" //每个项都要有初始化
                                , new String[]{en, cn, proficiency, plan, index});
                        LogUtils.v2("kong");
                    }
                    cn = "";
                }

            }
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }

    }
}